Algunos problemas de DP. Uno de ellos utiliza BigInteger e I/O de Java.
[and.git] / 11117 - Little Quilt / 11117.cpp
blob163c3e28662215528d0c69f66cf26011f8e64113
1 /*
2 Accepted
3 */
4 #include <iostream>
5 #include <vector>
6 #include <cassert>
9 using namespace std;
11 typedef vector<vector<char> > matrix;
14 string e;
15 int pos;
16 bool error;
17 matrix a, b, empty;
20 void match(const string &s){
21 int n = s.size();
22 for (int i=0; i<n; ++i){
23 assert(e[pos++] == s[i]);
27 void print(const matrix &a){
28 for (int i=0; i<a.size(); ++i){
29 for (int j=0; j<a[i].size(); ++j){
30 cout << a[i][j];
32 cout << endl;
34 //cout << endl;
38 matrix turn(const matrix &t){
39 matrix r;
40 for (int j = 0; j < t[0].size(); ++j){
41 r.push_back(vector<char>());
42 for (int i = t.size()-1; i >= 0; --i){
43 r.back().push_back(t[i][j]);
47 for (int i=0; i<r.size(); ++i){
48 for (int j = 0; j<r[i].size(); ++j){
49 if (r[i][j] == '/') r[i][j] = '\\';
50 else if (r[i][j] == '\\') r[i][j] = '/';
51 else if (r[i][j] == '-') r[i][j] = '|';
52 else if (r[i][j] == '|') r[i][j] = '-';
55 return r;
58 matrix sew(matrix x, const matrix &y){
59 if (x.size() != y.size()){
60 ::error = true;
61 return empty;
63 for (int i=0; i<y.size(); ++i){
64 for (int j=0; j<y[i].size(); ++j){
65 x[i].push_back(y[i][j]);
68 return x;
72 matrix Q(){
73 if (e[pos] == 'A'){
74 match("A");
75 return a;
76 }else if (e[pos] == 'B'){
77 match("B");
78 return b;
79 }else if (e[pos] == 't'){
80 match("turn(");
81 matrix t = Q();
82 if (::error) return empty;
83 match(")");
84 return turn(t);
85 }else if (e[pos] == 's'){
86 match("sew(");
87 matrix x = Q();
88 if (::error) return empty;
89 match(",");
90 matrix y = Q();
91 if (::error) return empty;
92 match(")");
93 matrix s = sew(x, y);
94 return s;
98 void initialize(){
99 a = vector<vector<char> >(2, vector<char>(2, '/'));
100 a[1][1] = '+';
102 //print(a);
104 b = vector<vector<char> >(2, vector<char>(2, '-'));
106 //print(b);
108 empty = vector<vector<char> >();
112 int main(){
113 int quiltNo = 1;
114 initialize();
115 while (!cin.eof()){
116 char c;
117 e = "";
118 pos = 0;
119 ::error = false;
120 while (cin >> c && c != ';'){
121 e += c;
123 if (e == "") break;
124 matrix q = Q();
126 cout << "Quilt " << quiltNo++ << ":" << endl;
127 //cout << e << endl;
128 if (::error){
129 cout << "error" << endl;
130 }else{
131 print(q);
134 return 0;